package co.edu.usbcali.dataaccess.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import co.edu.usbcali.dataaccess.api.HibernateDaoImpl;
import co.edu.usbcali.modelo.Articulos;
import co.edu.usbcali.modelo.dto.ArticulosDTO;
import co.edu.usbcali.modelo.dto.CategoriasDTO;
import co.edu.usbcali.modelo.restdto.ArticuloRestDTO;


/**
 * A data access object (DAO) providing persistence and search support for
 * Articulos entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @see lidis.Articulos
 */
@Scope("singleton")
@Repository("ArticulosDAO")
public class ArticulosDAO extends HibernateDaoImpl<Articulos, Long>
    implements IArticulosDAO {
    @SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ArticulosDAO.class);
    @Resource
    private SessionFactory sessionFactory;

    public static IArticulosDAO getFromApplicationContext(
        ApplicationContext ctx) {
        return (IArticulosDAO) ctx.getBean("ArticulosDAO");
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticulosDTO> articulosCategoriaUsuario(CategoriasDTO categoria)throws Exception {
		
		List<ArticulosDTO> listaArticulos=null;
		
		try {
			Query query = getSession().getNamedQuery("consultaArticulosCategoriaUsuario");
			query.setParameter("pCodCate", categoria.getCodigoCate());
			//query.setParameter("pCodUsu", categoria.getCodUsuario());
			query.setResultTransformer(Transformers.aliasToBean(ArticulosDTO.class));
			listaArticulos= query.list();

		} catch (Exception e) {
			throw e;
		}
		
		return listaArticulos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticulosDTO> articulosFavoritosUsuario(Long codUsu)
			throws Exception {
		
		List<ArticulosDTO> listaArticulos=null;
		
		try {
			Query query = getSession().getNamedQuery("consultaArticulosFavoritosUsuario");
			query.setParameter("pCodUsu", codUsu);
			query.setResultTransformer(Transformers.aliasToBean(ArticulosDTO.class));
			listaArticulos= query.list();

		} catch (Exception e) {
			throw e;
		}
		
		return listaArticulos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArticuloRestDTO buscarArticulo(Long codArticulo) throws Exception {
		
		List<ArticuloRestDTO> listaArticulos=null;
		ArticuloRestDTO articulo=null;
		try {
			Query query = getSession().getNamedQuery("consultaArticulo");
			query.setParameter("pCodArti", codArticulo);
			query.setResultTransformer(Transformers.aliasToBean(ArticuloRestDTO.class));
			listaArticulos= query.list();
			
			if(listaArticulos!=null && listaArticulos.size()>0){
				articulo=listaArticulos.get(0);
			}

		} catch (Exception e) {
			throw e;
		}
		
		return articulo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticuloRestDTO> traerArticulosFavoritos(Long codUsu)
			throws Exception {
            List<ArticuloRestDTO> listaArticulos=null;
		
		try {
			Query query = getSession().getNamedQuery("consultaArticulosFavoritosUsuario");
			query.setParameter("pCodUsu", codUsu);
			query.setResultTransformer(Transformers.aliasToBean(ArticuloRestDTO.class));
			listaArticulos= query.list();

		} catch (Exception e) {
			throw e;
		}
		
		return listaArticulos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticuloRestDTO> traerArticulosCategoria(Long codCategoria)
			throws Exception {
		List<ArticuloRestDTO> listaArticulos=null;
		
		try {
			Query query = getSession().getNamedQuery("consultaArticulosCategoriaUsuario");
			query.setParameter("pCodCate", codCategoria);
			//query.setParameter("pCodUsu", categoria.getCodUsuario());
			query.setResultTransformer(Transformers.aliasToBean(ArticuloRestDTO.class));
			listaArticulos= query.list();

		} catch (Exception e) {
			throw e;
		}
		
		return listaArticulos;
	}
}
